import { createRouter, createWebHashHistory } from 'vue-router'
import { test } from './network/api/home'
import Login from './pages/login'
import Index from './pages/home/home'
const Home = () => import('./pages/home/index')
const Edit = () => import('./pages/edit')
const Tags = () => import('./pages/tags')
const Msg = () => import('./pages/msg')
const Feiend = () => import('./pages/friend')

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home,
        redirect: '/home/index',
        meta: {
            requireAuth: true
        },
        children: [
            {
                path: 'index',
                component: Index,
                meta: {
                    requireAuth: true
                }

            },
            {
                path: 'edit',
                component: Edit,
                meta: {
                    requireAuth: true
                }
            },
            {
                path: 'tags',
                component: Tags,
                meta: {
                    requireAuth: true
                }
            },
            {
                path: 'msg',
                component: Msg,
                meta: {
                    requireAuth: true
                }
            },
            {
                path: 'friend',
                component: Feiend,
                meta: {
                    requireAuth: true
                }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach(async (to, from, next) => {
    if (to.meta.requireAuth) {
        await test()
        next()
    } else {
        next()
    }
})

export default router